//
//  MagicCardFunctionViewController.m
//  MagicCardDemo
//
//  Created by 向恒 on 15/6/27.
//  Copyright (c) 2015年 向恒. All rights reserved.
//



#import "MagicCardFunctionViewController.h"
//#import "CommonFunctionSettingsTableViewController.h"
#import "CommonFunctionSettingsTableDetailsViewController.h"
#import "ASIHTTPRequest.h"
#import "AppDelegate.h"
#import "CardSerialize.h"

#import "UploadFileList.h"
//#import "NaviTestTableViewController.h"
@interface MagicCardFunctionViewController ()

@end

@implementation MagicCardFunctionViewController
@synthesize functions;
@synthesize details;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"功能";
    //@"复习个数",@"记录显示方式",@"所有记录",@"复习次数",
    //尽量少让用户进行设置
    functions = [NSMutableArray arrayWithObjects:@"本月签到XX次",@"数字对照表（0～100）",@"桩子对照表(0-9)",@"设置",@"小贴士",@"上传数据",@"下载数据",nil];
    //20160126
    //记录页面只显示刷新的记录
    //比如今天5个每分钟，明天6个每分钟，那么两个记录都保留在这个页面
    //如果今天5个每分钟，明天4个每分钟，那么明天的记录就不保留在这个页面
    //还存在一个所有记录的页面
    //还需要添加的功能：
    //第一次练习之后还需要（1，2，3，4，5）次复习。
    //记录现实的方式
    //比如按最好成绩降序（升序）排列，按练习个数降序（升序）排列，按消耗时间降序（升序）排列
    //小贴士
    //提示需要注意的一些原则
    
    //functions = [NSMutableArray arrayWithObjects:@"通用",@"设置记录显示",@"关于",nil];
    //details = [NSMutableArray arrayWithObjects:@"",@"",@"", nil];
//    self.navigationController.title  = @"function list";
    
    self.table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    self.table.dataSource = self;
    self.table.delegate = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//
//-(void) viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    //[self.tableView reloadData];
//}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"cellId";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSUInteger rowNo =indexPath.row;
    cell.textLabel.text = [functions objectAtIndex:rowNo];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    //cell.detailTextLabel.text  = [details objectAtIndex:rowNo];
    return  cell;
    
}


-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  functions.count;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    switch (row) {
        case 1://数字对照表
        {
//            CommonFunctionSettingsTableViewController* cfs = [[CommonFunctionSettingsTableViewController alloc]init];
//            cfs.title = @"通用";
//            NSLog(@"%@",self.navigationController);
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:cfs animated:YES];
            CommonFunctionSettingsTableDetailsViewController* cfstt = [[CommonFunctionSettingsTableDetailsViewController alloc]init];
            cfstt.title = @"对照表（0～100）";
            NSLog(@"%@",self.navigationController);
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cfstt animated:YES];
        }
            break;
        case 2://桩子表
            break;
        case 5://上传数据
            //读取待上传文件列表，上传，删除待上传列表
        {
            AppDelegate* ad = [UIApplication sharedApplication].delegate;  //需要使用公用的数据。
            if([ad.recordFileList count] <= 0) return;
            
            UploadFileList * ufl = [[UploadFileList alloc]init];
            NSMutableString* filename = nil;
            while((filename =[ufl Get]) != nil)
            {
                if([filename isEqual:@""]) return;
                CardQueue* recordQueue = [CardData ReadDataFromFile:filename];
                NSMutableString * str = @"";
                for(int i = 0;i < recordQueue.arrCards.count;i++)
                {
                    NSNumber * num = [recordQueue.arrCards objectAtIndex:i];
                    NSString * strNum = [NSString stringWithFormat:@"%@",num];
                    str = [str stringByAppendingString:strNum];
                    if(i<recordQueue.arrCards.count-1)
                        str = [str stringByAppendingString:@","];
                    
                }
                
                
                NSDictionary* user = [[NSDictionary alloc] initWithObjectsAndKeys:str,@"origin_record",
                                      [NSString stringWithFormat:@"%d",recordQueue.nCostTime],@"cost_time",
                                      recordQueue.date,@"record_time",
                                      nil];
                if([NSJSONSerialization isValidJSONObject:user])
                {
                    NSError* error;
                    NSData* jsonData =
                    [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error:&error];
                    NSMutableData* tempJsonData = [NSMutableData dataWithData:jsonData];
                    //这里还是太麻烦了，还是得有一个配置文件来专门存储IP和port
                    //不做外网相关的内容的话，暂时不用ngork了
                    NSURL* url = [NSURL URLWithString:@"http://localhost:8000/post/"];
                    ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
                    [request addRequestHeader:@"Content-Type" value:@"application/json;encoding=utf-8"];
                    [request addRequestHeader:@"Accept" value:@"application/json"];
                    [request setRequestMethod:@"POST"];
                    [request setPostBody:tempJsonData];
                    [request startSynchronous];
                    NSError* error1 = [request error];
                    if(!error1)
                    {
                        NSString* response = [request responseString];
                        NSLog(@"Test:%@",response);
                    }
                }
                
            }
            
            //NSString* file =  [ad.recordFileList objectAtIndex:0];

            

            

        }
            
            
            break;
        case 6: //下载数据
            break;
        default:
            break;
    }
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.hidesBottomBarWhenPushed = NO;
}

//-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
//{
//    NSInteger rowNo = indexPath.row;
//    
//    
//}


@end
